-- @owner:yangjian
-- @date:2024-4-3
-- @testpoint:1.去除大对象使用限制，验证在事务中，大对象操作遇错回滚，合理报错

--清理环境
CREATE OR REPLACE FUNCTION f_opengauss_large_object_use_case0004_1()  
RETURNS VOID AS 
$$
BEGIN  
    BEGIN
    PERFORM lo_unlink(100);
    EXCEPTION
        WHEN others THEN
            NULL;
    end;
    BEGIN
    PERFORM lo_unlink(101);
    EXCEPTION
        WHEN others THEN
            NULL;
    end;
    BEGIN
    PERFORM lo_unlink(200);
    EXCEPTION
        WHEN others THEN
            NULL;
    end;
END;  
$$
LANGUAGE plpgsql;
/

select f_opengauss_large_object_use_case0004_1();

--环境准备
drop table if exists t_opengauss_large_object_use_case0004_1 cascade;
CREATE TABLE t_opengauss_large_object_use_case0004_1 (loid oid,fd int);
INSERT INTO t_opengauss_large_object_use_case0004_1 (loid) SELECT lo_create(100);

--step1:lo_creat函数在事务中遇错回滚;expect:失败
begin;
SELECT lo_creat('a');
end;
/
end;
--step2:lo_create函数在事务中遇错回滚;expect:失败
begin;
SELECT lo_create('a');
end;
/
end;

--step3:函数lo_open遇错回滚;expect:失败
begin;
SELECT lo_open('a',CAST(x'20000' | x'40000' AS integer));
select lowrite (0,'aaaaaaaaaa');
select loread (0, 6);
select lo_close (0);
end;
/
end;
--step4:函数lowrite遇错回滚;expect:失败
begin;
SELECT lo_open(100,CAST(x'20000' | x'40000' AS integer));
select lowrite (1,'aaaaaaaaaa');
select loread (0, 6);
select lo_close (0);
end;
/
end;
--step5:函数loread遇错回滚;expect:失败
begin;
SELECT lo_open(100,CAST(x'20000' | x'40000' AS integer));
select lowrite (0,'aaaaaaaaaa');
select loread (1, 6);
select lo_close (0);
end;
/
end;
--step6:函数lo_close遇错回滚;expect:失败
begin;
SELECT lo_open(100,CAST(x'20000' | x'40000' AS integer));
select lowrite (0,'aaaaaaaaaa');
select loread (0, 6);
select lo_close (1);
end;
/
end;
--step7:函数lo_lseek遇错回滚;expect:失败
begin;
SELECT lo_open(100,CAST(x'20000' | x'40000' AS integer));
select lo_lseek(1,5,0);
select lo_close (0);
end;
/
end;
--step8:函数lo_lseek64遇错回滚;expect:失败
begin;
SELECT lo_open(100,CAST(x'20000' | x'40000' AS integer));
select lo_lseek64(1,5,5);
select lo_close (0);
end;
/
end;
--step9:函数lo_tell遇错回滚;expect:失败
begin;
SELECT lo_open(100,CAST(x'20000' | x'40000' AS integer));
select lo_tell(1);
select lo_close (0);
end;
/
end;
--step10:函数lo_tell64遇错回滚;expect:失败
begin;
SELECT lo_open(100,CAST(x'20000' | x'40000' AS integer));
select lo_tell64(1);
select lo_close (0);
end;
/
end;
--step11:函数lo_truncate遇错回滚;expect:失败
begin;
SELECT lo_open(100,CAST(x'20000' | x'40000' AS integer));
select lowrite (0,'aaaaaaaaaa');
select lo_truncate(1,2);
select lo_close (0);
end;
/
end;
--step12:函数lo_truncate遇错回滚;expect:失败
begin;
SELECT lo_open(100,CAST(x'20000' | x'40000' AS integer));
select lowrite (0,'aaaaaaaaaa');
select lo_truncate64(1,1);
select lo_close (0);
end;
/
end;
--step13:函数lo_put遇错回滚;expect:失败
begin;
select lo_put('a',6,'aaaaaa');
select lo_get(100,6,6);
end;
/
end;
--step14:函数lo_get遇错回滚;expect:失败
begin;
select lo_put(100,6,'aaaaaa');
select lo_get('a',6,6);
end;
/
end;
--step15:函数lo_from_bytea遇错回滚;expect:失败
begin;
select lo_from_bytea('a','aaaaaa');
end;
/
end;
--step16:函数lo_unlink遇错回滚;expect:失败
begin;
select lo_unlink(200);
end;
/
end;
--清理环境
select lo_unlink(loid) from pg_largeobject;
select f_opengauss_large_object_use_case0004_1();
drop table if exists t_opengauss_large_object_use_case0004_1 cascade;
drop function f_opengauss_large_object_use_case0004_1();
